package DataAnalysis_qcl

import org.apache.spark.sql.SparkSession

import java.util.Properties

object DataAnalysis04 {
  def main(args: Array[String]): Unit = {
    /*
          DataAnalysis04:分析的整个北京租房信息里面房屋类型的数量
     */
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("DataAnalysis04")
      .enableHiveSupport()
      .getOrCreate()

    val mysql_connect=new Properties()
    mysql_connect.setProperty("user","root")
    mysql_connect.setProperty("password","123456")
    mysql_connect.setProperty("driver","com.mysql.jdbc.Driver")

    spark.sql("use qcl")

    spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/qcl?useSSL=false","data_processing",mysql_connect)
      .createOrReplaceTempView("data")

    spark.sql("use qcl")
   val result= spark.sql(
      """
        |select
        |house_type,
        |count(*) as number
        |from data
        |group by house_type
        |order by number desc
        |""".stripMargin)

    result.write.mode("overwrite")
      .jdbc("jdbc:mysql://192.168.40.110:3306/qcl?useSSL=false", "data_analysis04", mysql_connect)

    //  将结果存入hdfs
    result.repartition(1).write.mode("overwrite")
      .csv("/qcl/data_analysis/data_analysis04.csv")


    spark.close()
  }

}
